package com.cskaoyan.config;

import com.cskaoyan.interceptor.HandlerInterceptor1;
import com.cskaoyan.interceptor.HandlerInterceptor2;
import com.cskaoyan.interceptor.HandlerInterceptor3;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author stone
 * @date 2022/06/01 16:26
 */
@EnableWebMvc //mvc:annotation-driven; 也会把当前类注册为容器中的组件； 也会当做是配置类
@ComponentScan("com.cskaoyan.controller")
public class MvcConfiguration implements WebMvcConfigurer {
    //Converter
    @Override
    public void addFormatters(FormatterRegistry registry) {

    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 静态资源映射 → mapping和location
        // 要访问 d:/stone/spring/dlrb.jpg    localhost:8080/pic/dlrb.jpg
        registry.addResourceHandler("/pic/**").addResourceLocations("file:d:/stone/spring/");
        // registry.addResourceHandler().addResourceLocations();
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // HandlerInterceptor是谁？作用范围是什么？顺序？
        registry.addInterceptor(new HandlerInterceptor1()); // 作用范围是/**
        registry.addInterceptor(new HandlerInterceptor2()).addPathPatterns("/user/**");
        registry.addInterceptor(new HandlerInterceptor3()).addPathPatterns("/user/**");
    }
}
